/*
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package au.com.domlogic.helloPVCWorld;

import au.com.domlogic.pvc.core.element.PVCElement;
import au.com.domlogic.pvc.core.servlet.LocalRedirectionResponseWrapper;
import au.com.domlogic.pvc.xhtml.servlet.XHTMLStrictRequestWrapper;
import au.com.domlogic.pvc.xhtml.servlet.XHTMLStrictServlet;
import au.com.domlogic.pvc.xhtml.attr.ClassAttrMold;
import au.com.domlogic.pvc.xhtml.document.XHTMLStrictDocument;
import au.com.domlogic.pvc.xhtml.element.DivElementMold;
import au.com.domlogic.pvc.xhtml.element.PElementMold;
import au.com.domlogic.pvc.xhtml.element.builder.CSSLinkMoldBuilder;
import au.com.domlogic.pvc.xhtml.element.builder.JavaScriptMoldBuilder;
import au.com.domlogic.pvc.xhtml.element.scripts.JQueryCoreMoldBuilder;
import au.com.domlogic.pvc.xhtml.element.scripts.JQueryUIMoldBuilder;
import au.com.domlogic.pvc.xhtml.serialization.XHTMLStrictSerializer;
import au.com.domlogic.helloPVCWorld.builder.HelloWorldHeadingMoldBuilder;
import au.com.domlogic.pvc.xhtml.element.AElementMold;
import au.com.domlogic.pvc.xhtml.element.builder.AnchorMoldBuilder;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.xml.transform.TransformerException;

/**
 *
 * @author Domenic Figliomeni
 */
public class HelloServlet extends XHTMLStrictServlet {

    @Override
    protected void doGet(XHTMLStrictRequestWrapper request, LocalRedirectionResponseWrapper response)
            throws ServletException, IOException {

        //Can use a simple dom method to set a title
        request.getDocument().getTitle().setTextContent("Hello World");

        XHTMLStrictDocument document = request.getDocument();
        PVCElement body = document.getBody();
        PVCElement head = document.getHead();

        //Decorate the body with a preconfigured heading
        HelloWorldHeadingMoldBuilder headingMoldBuilder = new HelloWorldHeadingMoldBuilder();
        body = headingMoldBuilder.buildElementMold().new ElementDecorator(body);

        //Create a draggable div mold in the body
        DivElementMold dragDiv =
                new DivElementMold().addAttrMold(
                new ClassAttrMold("draggable")).addComponentMold(
                new PElementMold().setContent("Drag Me"));
        body = dragDiv.new ElementDecorator(body);

        //Create a droppable div in the body
        DivElementMold dropDiv =
                new DivElementMold().addAttrMold(
                new ClassAttrMold("droppable")).addComponentMold(
                new PElementMold().setContent("Drop on me"));
        body = dropDiv.new ElementDecorator(body);

        //Decorate the head with molds for common script dependencies
        head = new JQueryCoreMoldBuilder().buildElementMold().new ElementDecorator(head);
        head = new JQueryUIMoldBuilder().buildElementMold().new ElementDecorator(head);

        //Decorate the head with a specific script for this document
        JavaScriptMoldBuilder javaScriptBuilder = new JavaScriptMoldBuilder("javascript/hello.js");
        head = javaScriptBuilder.buildElementMold().new ElementDecorator(head);

        //Decorate the head with a link to the stylesheet
        CSSLinkMoldBuilder cssLinkBuilder = new CSSLinkMoldBuilder("css/hello.css");
        head = cssLinkBuilder.buildElementMold().new ElementDecorator(head);

        //Build an anchor to the example
        AnchorMoldBuilder anchorBuilder = new AnchorMoldBuilder();
        anchorBuilder.setLabel("Example");
        anchorBuilder.setHref("Example");
        AElementMold anchor = anchorBuilder.buildElementMold();
        body = anchor.new ElementDecorator(body);

        //Call the decorator method gluePVC() on the PVCElements to execute the decorator functionality
        head.gluePVC();
        body.gluePVC();

        //Serialize
        try {
            XHTMLStrictSerializer.serialize(document, response.getOutputStream());
        }
        catch (TransformerException e) {
            throw new ServletException(e);
        }
    }
}
